#include "unistd.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"

int main()
{
    int fd = open("love.txt",O_RDONLY);
    if (fd == -1)
    {
        perror("open");
        // 失败退出 => 相当于return -1
        exit(EXIT_FAILURE);
    }

    int fd_write = open("love_copy.txt",O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (fd_write == -1)
    {
        perror("open");
        close(fd);
        exit(EXIT_FAILURE);
    }

    // 读取文件
    char buf[100];
    ssize_t read_len = 0;
    while ((read_len = read(fd, buf, sizeof(buf))) > 0)
    { 
        write(fd_write, buf, read_len);
        write(STDOUT_FILENO, buf, read_len);
    }

    close(fd);
    close(fd_write);
    return 0;
}